<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #74ebd5, #acb6e5);
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        
        .container {
            width: 100%;
            max-width: 400px;
            padding: 20px;
        }
        
        .register-box {
            width: 500px;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            padding: 30px;
            text-align: center;
        }
        
        .register-box h2 {
            color: #333;
            margin-bottom: 30px;
            font-size: 2.5rem;
        }
        
        .input-group {
            margin-bottom: 20px;
            text-align: left;
        }
        
        .input-group label {
            display: block;
            margin-bottom: 5px;
            color: #666;
            font-weight: 500;
        }
        
        .input-group input {
            width: 95%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }
        
        .input-group input:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
        }
        
        button {
            width: 100%;
            padding: 12px;
            background: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            font-size: 1rem;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        
        button:hover {
            background: #0056b3;
        }
        
        .login-link {
            margin-top: 20px;
            font-size: 0.9rem;
            color: #666;
            text-align: center;
        }
        
        .login-link a {
            color: #007bff;
            text-decoration: none;
            font-weight: bold;
        }
        
        .login-link a:hover {
            text-decoration: underline;
        }
        
        .error-message {
            color: red;
            font-size: 0.9rem;
            margin-top: 5px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="register-box">
            <h2>用户注册</h2>
            <form id="registerForm">
                <div class="input-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" required>
                    <span class="error-message" id="usernameError"></span>
                </div>
                <div class="input-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required placeholder="6~10个字符，含有大小写字母+数字">
                    <span class="error-message" id="passwordError"></span>
                </div>
                <div class="input-group">
                    <label for="email">邮箱</label>
                    <input type="email" id="email" name="email" required>
                    <span class="error-message" id="emailError"></span>
                </div>
                <button type="submit">注册</button>
                <p class="login-link">已有账号？ <a href="../登录/登录.html">登录</a></p>
            </form>
        </div>
    </div>

    <script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function() {

            $('#registerForm').submit(function(event) {
                event.preventDefault();


                const username = $('#username').val();
                const password = $('#password').val();
                const email = $('#email').val();


                $('.error-message').text('');


                if (!username || !password || !email) {
                    alert('请填写所有字段');
                    return;
                }


                $.ajax({
                    type: 'POST',
                    url: 'http://47.94.4.201/index.php/index/index/register',
                    dataType: 'json',
                    data: {
                        name: username,
                        pwd: password,
                        emails: email,
                    },
                    success: function(data) {

                        if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,10}$/.test(password)) {
                            alert("密码格式不正确");
                            return;
                        }

                        console.log(data);
                        if (data.msg === '注册成功') {
                            alert('注册成功！');
                            location.assign('../登录/登录.html');
                        } else {
                            if (data.msg === '用户名重复') {
                                $('#usernameError').text('*用户名已存在');
                            } else {
                                alert('注册成功！');
                                location.assign('../登录/登录.html');
                            }
                        }
                    },
                    error: function(e) {
                        console.log(e);
                        alert('请求失败，请稍后重试');
                    }
                });
            });
        });
    </script>
</body>

</html>